package com.alks.function.controller;

import com.alks.common.aop.ApiRestController;
import com.alks.common.aop.WebLogger;
import com.alks.common.data.dto.SysUserTokenDTO;
import com.alks.common.data.thread.UserIdThread;
import com.alks.common.service.RedisService;
import com.alks.common.utils.JWTUtil;
import com.alks.common.utils.beanutils.StringUtils;
import com.alks.entity.data.response.ResponseInfo;
import com.alks.entity.exception.TokenErrorException;
import com.alks.function.data.request.sys.SysUserRequest;
import com.alks.function.data.vo.sys.ThirdUserInfoVO;
import com.alks.function.service.SdUserService;
import com.alks.function.service.SysUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;

@ApiRestController("/user")
@Api(tags = {"用户管理"})
@Slf4j
public class SysUserController {
    @Autowired
    private SysUserService userService;

    @Autowired
    private SdUserService sdUserService;
    @Autowired
    RedisService redisService;

    @WebLogger
    @PostMapping("/login")
    @ApiOperation(value = "用户登录",tags = "用户管理")
    @ApiResponses({
            @ApiResponse(code = 200 ,message = "ok",response = ResponseInfo.class)
    })
    public ResponseInfo login(@RequestBody SysUserRequest sysUser,
                              HttpServletRequest servletRequest){
        return userService.login(sysUser,servletRequest);
    }

    @WebLogger
    @PostMapping("/updatePassword")
    @ApiOperation(value = "用户修改登录密码",tags = "用户管理")
    public ResponseInfo updatePassword(@RequestParam String userId, @RequestParam String oldPassword, @RequestParam String newPassword){
        return sdUserService.updatePasswordLogin(userId,oldPassword,newPassword);
    }

    @WebLogger
    @PostMapping("/progQuery")
    @ApiOperation(value = "展示功能查询",tags = "用户管理")
    public ResponseInfo progQuery(){
        return userService.progQuery();
    }

    @WebLogger
    @GetMapping("/checkToken")
    @ApiOperation(value = "验证token",tags = "用户管理")
    public ThirdUserInfoVO checkToken() {
        ThirdUserInfoVO infoVO = new ThirdUserInfoVO();
        infoVO.setUsername(UserIdThread.get().getLoginName());
        infoVO.setNickname(UserIdThread.get().getUserName());
        infoVO.setDeptId(UserIdThread.get().getDeptId());
        return infoVO;
    }

}
